package com.cyy.partner.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cyy.partner.common.Result;
import com.cyy.partner.pojo.Category;
import com.cyy.partner.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: cyy
 * @Date: 2024/5/2
 */
@RestController
@RequestMapping("/category")
@Api(tags = "活动分类管理相关接口")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 添加分类
     * @param category
     * @return
     */
    @ApiOperation("活动分类添加接口")
    @PostMapping("/add")
    public Result<String> addCategory(@RequestBody Category category){
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getCategoryName, category.getCategoryName());
        Category cg = categoryService.getOne(queryWrapper);
        if(cg != null){
            return Result.error("该分类已存在");
        }else{
            category.setCreateTime(LocalDateTime.now());
            categoryService.save(category);
            return Result.success("添加分类成功");
        }
    }

    /**
     * 获取所有分类信息
     * @return
     */
    @ApiOperation("活动分类列表列表")
    @GetMapping("/list")
    public Result<List<Category>> list(){
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Category::getId);
        List<Category> list = categoryService.list(queryWrapper);
        return Result.success(list);
    }

    /**
     * 删除分类
     * @param categoryName
     * @return
     */
    @ApiOperation("根据分类名删除分类接口")
    @DeleteMapping
    public Result<String> delete(String categoryName){
        System.out.println(categoryName);
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getCategoryName, categoryName);
        List<Category> list = categoryService.list(queryWrapper);
        if(list.size()==0){
            return Result.error("没有该分类");
        }else{
            categoryService.remove(queryWrapper);
            return Result.success("分类删除成功");
        }
    }

}
